﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using System.Collections.ObjectModel;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;
using ChinaCustoms.Qingdao.Workflow.Controls;

public partial class Utility_RoleSelector : System.Web.UI.Page
{
    string ApplicationCodeName = string.Empty;
    //PermissionClient permissionClient;

    protected void SelectButton_Click(object sender, EventArgs e)
    {
        string selectedArray = string.Empty;
        for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
        {
            if (this.CheckBoxList1.Items[i].Selected == true)
            {
                selectedArray += string.Format("{0},", this.CheckBoxList1.Items[i].Value);
            }
        }

        if (selectedArray.EndsWith(","))
        {
            selectedArray = selectedArray.Substring(0, selectedArray.Length - 1);
        }

        //string returnValue = string.Format("new Array({0})", selectedArray);
        string returnValue = string.Format("\"{0}\"", selectedArray);
        ModalUtil.ClosePage(Page, returnValue);
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        ModalUtil.ClosePage(Page);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.ApplicationCodeName = ConfigurationManager.AppSettings["ApplicationCodeName"];
        //permissionClient = new PermissionClient("iisHostEndpoint");
        if (this.IsPostBack == false)
        {
            BindRoles();
        }
    }


    protected void BindRoles()
    {
        if (string.IsNullOrEmpty(ApplicationCodeName) == false)
        {
            ReadOnlyCollection<IRole> roleColl = ChinaCustomsPermissionWrapper.GetRolesInApplication(ApplicationCodeName);
            this.CheckBoxList1.Items.Clear();
            for (int i = 0; i < roleColl.Count; i++)
            {
                ListItem item = new ListItem();
                item.Text = roleColl[i].Name + "[" + roleColl[i].CodeName + "]";
                item.Value = roleColl[i].CodeName;
                this.CheckBoxList1.Items.Add(item);
            }
        }
    }
}
